package com.mingsoft.led.comm;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.Socket;

import com.mingsoft.led.Cmd;
import com.mingsoft.led.util.Configure;
import com.mingsoft.led.util.Utils;

public class CommThread extends Thread {
	private Socket socket = null;
	private boolean running = false;

	public CommThread(Socket s) {
		socket = s;
	}

	public void start() {
		try {
			new DefaultInputThread(socket.getInputStream()).start();
			super.start();
		} catch (IOException e) {
			e.printStackTrace();
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e2) {
				}
				socket = null;
			}
		}
	}

	public void run() {
		running = true;
		try {
			write("init{"
					+ Configure.getInstance().get(Configure.KEY_WELCOME_PATH)
					+ "\n");
			System.out.println("init{"
					+ Configure.getInstance().get(Configure.KEY_WELCOME_PATH)
					+ "\n");
			while (running && socket != null) {
				Robot robot = null;
				if (!CommQueue.getInstance().getCommands().isEmpty()) {
					Cmd cmd = CommQueue.getInstance().getCommands().poll();
					System.out.println(cmd.getCmd() + "\t" + cmd.getParam());
					try {
						switch (cmd.getCmd()) {
						case CLOSE:
							write("close\n");
							break;
						case VIDEO_OPEN:
							write("openvideo{" + cmd.getParam() + "\n");
							break;
						case VIDEO_PAUSE:
							write("videopause\n");
							break;
						case VIDEO_PLAY:
							write("videoplay\n");
							break;
						case VIDEO_VOLUMN_UP:
							write("videovolumnup\n");
							break;
						case VIDEO_VOLUMN_DOWN:
							write("videovolumndown\n");
							break;
						case PPT_OPEN:
							write("openppt{" + cmd.getParam() + "\n");
							break;
						case PPT_NEXT:
							try {
								robot = new Robot();
								robot.keyPress(KeyEvent.VK_RIGHT);
								robot.keyRelease(KeyEvent.VK_RIGHT);
							} catch (Exception e) {
								e.printStackTrace();
							}
							break;
						case PPT_PREVIOUS:
							try {
								robot = new Robot();
								robot.keyPress(KeyEvent.VK_LEFT);
								robot.keyRelease(KeyEvent.VK_LEFT);
							} catch (Exception e) {
								e.printStackTrace();
							}
							break;
						case PICTURE_OPEN:
							write("openpicture{" + cmd.getParam() + "\n");
							break;
						case PICTURE_NEXT:
							write("nextpicture\n");
							break;
						case PICTURE_PREVIOUS:
							write("previouspicture\n");
							break;
						case CAMERA_OPEN:
							write("opencamera{" + cmd.getParam() + "\n");
							break;
						case CAMERA_MOVE_LEFT_TOP:
							write("cameramovelefttopon\n");
							break;
						case CAMERA_MOVE_TOP:
							write("cameramovetopon\n");
							break;
						case CAMERA_MOVE_RIGHT_TOP:
							write("cameramoverighttopon\n");
							break;
						case CAMERA_MOVE_RIGHT:
							write("cameramoverighton\n");
							break;
						case CAMERA_MOVE_RIGHT_DOWN:
							write("cameramoverightdownon\n");
							break;
						case CAMERA_MOVE_DOWN:
							write("cameramovedownon\n");
							break;
						case CAMERA_MOVE_LEFT_DOWN:
							write("cameramoveleftdownon\n");
							break;
						case CAMERA_MOVE_LEFT:
							write("cameramovelefton\n");
							break;
						case CAMERA_ZOOM_IN:
							write("camerazoomin\n");
							break;
						case CAMERA_ZOOM_OUT:
							write("camerazoomout\n");
							break;
						case CAMERA_FOCUS_IN:
							write("camerafocusin\n");
							break;
						case CAMERA_FOCUS_OUT:
							write("camerafocusout\n");
							break;
						case CAMERA_IRIS_OPEN:
							write("camerairisopen\n");
							break;
						case CAMERA_IRIS_CLOSE:
							write("camerairisclose\n");
							break;
						case UNKOWN:
							break;
						default:
							break;
						}
						socket.getOutputStream().flush();
					} catch (Exception e) {
						e.printStackTrace();
						running = false;
						break;
					}
				} else {
					Utils.sleep(100);
				}
			}
			running = false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
				}
				socket = null;
			}
			running = false;
		}
	}

	public void write(String str) throws Exception {
		socket.getOutputStream().write(str.getBytes("UTF-8"));
	}

	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	public boolean isRunning() {
		return running;
	}

	public void setRunning(boolean running) {
		this.running = running;
	}

}
